package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.common.utils.PageRequestUtil;
import cloud.maque.module.system.entity.SysDictType;
import cloud.maque.module.system.mapper.DictTypeMapper;
import cloud.maque.module.system.service.DictTypeService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, SysDictType> implements DictTypeService {


    @Autowired
    PageRequestUtil util;

    @Autowired
    TokenUserUtil userUtil;

    @Override
    public IPage<SysDictType> GetList(PageRequest<SysDictType> pageReq) {
        Page<SysDictType> page = util.GetPage(SysDictType.class, pageReq);
        QueryWrapper<SysDictType> query = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getDictName())) {
            query.lambda().like(SysDictType::getDictName, pageReq.getData().getDictName());
        }
        if (!StrUtil.isEmptyIfStr(pageReq.getData().getDictCode())) {
            query.lambda().like(SysDictType::getDictCode, pageReq.getData().getDictCode());
        }

        query.lambda().orderByDesc(SysDictType::getCreateTime);
        return this.page(page, query);
    }

    @Override
    public SysDictType SaveDictType(SysDictType data) {
        Assert.hasText(data.getDictName(), "字典名称不能为空");
        Assert.hasText(data.getDictCode(), "字典编码不能为空");


        if (StrUtil.isEmptyIfStr(data.getDictId())) {
            data.setCreateBy(userUtil.GetUserName());
            data.setCreateTime(new Date());
            data.setUpdateBy(data.getCreateBy());
            data.setUpdateTime(new Date());
        } else {
            SysDictType oldPost = this.baseMapper.selectById(data.getDictId());
            if (oldPost != null) {
                data.setCreateTime(oldPost.getCreateTime());
                data.setCreateBy(oldPost.getCreateBy());
            }
            data.setUpdateBy(userUtil.GetUserName());
            data.setUpdateTime(new Date());

        }

        SysDictType oldPost = GetByCode(data.getDictCode());
        if (oldPost != null && !oldPost.getDictId().equals(data.getDictId())) {
            throw new BusinessException("字典编码已存在");
        }

        this.saveOrUpdate(data);
        return data;
    }

    @Override
    public Boolean DeleteType(String dictId) {
        return this.removeById(dictId);
    }

    @Override
    public SysDictType GetByCode(String code) {

        QueryWrapper<SysDictType> query = new QueryWrapper<>();
        query.lambda().eq(SysDictType::getDictCode, code);
        return this.getOne(query, false);
    }
}
